package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.WeatherMapper;
import com.iretailer.report.model.Companies;
import com.iretailer.report.model.Weathers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/1/24 13:23
 * @Description:
 */
@Service
public class WeatherService extends ServiceImpl<WeatherMapper, Weathers> {
    @Autowired
    private WeatherMapper weatherMapper;

    /***
     * 插入 替换 天气情况
     * @param weathers
     * @return
     */
    public int replaceWeather(List<Weathers> weathers) {
        return weatherMapper.replaceWeather(weathers);
    }

    public PageResult<Weathers> gridPage(String cityName, String date, QueryOption queryOption) {
        Page<Weathers> page = new Page(1, Long.MAX_VALUE);
        QueryWrapper<Weathers> queryWrapper = new QueryWrapper<>();
        if (cityName != null) {
            queryWrapper.like("cityName", cityName);
        }
        if (date != null) {
            queryWrapper.like("date", date);
        }
        queryWrapper.orderByDesc("date");
        queryOption.judge(page, queryWrapper);
        IPage pageInfo = weatherMapper.selectPage(page, queryWrapper);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getRecords());
    }

    public List<Weathers> selectWeathersBy_cityId_dates(String cityId, String startDate, String endDate) {
        return weatherMapper.selectWeathersBy_cityId_dates(cityId, startDate, endDate);
    }
}
